<template>
    <el-row class="user-info-form" type="flex" justify="center">
        <el-col>
			<div class="asset-info-form">
				<el-form :model="organizeForm" :rules="organizeForm" ref="organizeForm" @submit.native.prevent>
					<el-form-item label="">
					<el-tree
						v-loading="loading"
						:data="organizeFormlist"
						:props="orgDefaultProps"
						node-key="accountName"
						highlight-current
						default-expand-all
						:expand-on-click-node="false"
						show-checkbox
						check-on-click-node
						ref="tree"
						@check="checkSelect">
					</el-tree>
					</el-form-item>
					<el-form-item label-width="0" class="verifyidentity-button">
						<el-button @click="closeDialog()" round>取消</el-button>
						<el-button type="primary" @click="quit('organizeForm')" round>确定</el-button>
					</el-form-item>
				</el-form>
			</div>
        </el-col>
    </el-row>
</template>

<script>

import util from '@/utils/util'
import { getHaveOrgUserList,updateMaitainAccount } from '@/api/maintence'

export default {
	name:'EditMaintence',
	props:{
		scopeRow: {
	      	// type: Object,
	      	required: false
	    },
		multipleCheck: {
	      	type: Array,
	      	required: false
	    },
	    closeDialog: {
	        type: Function,
	        required: true
	    }
	},
	components: {},
	data() {
		return {
			organizeForm:{},
			organizeFormlist:[],
			orgDefaultProps:{
				// children: 'nodes',
		        label:function(data, node){
					return util.transNullundefined(data.name) + " - " + util.transNullundefined(data.postName);
		        },
		        value: 'accountName'
			},
			checkedArr:[],
			loading: false,
			newTaskId:""
		}
	},
	created:function(){
		let infoObj = this.scopeRow
		let orgId = '';
		//点击了批量按钮-数组
		if(Array.isArray(infoObj)){
			orgId = infoObj[0].orgId;
			if(this.multipleCheck.length>0){
				this.newTaskId = this.multipleCheck.toString();
			}
		}
		//说明是点击行数据
		else{
			orgId = infoObj.orgId;
			this.newTaskId = infoObj.id;
		}
		
		this.getOrganizeFormlist(orgId);
	},
	methods:{
		getOrganizeFormlist(orgId) {
			let self = this;
			self.loading = true;
		  	getHaveOrgUserList({
		  		orgIds:orgId?orgId:"",
		  		roleIds:"185,184,183",
		  	}).then((res) => {
				 self.organizeFormlist = res.data.length>0 ?res.data : []
				if(!(Array.isArray(self.scopeRow))){
					self.$refs.tree.setCheckedKeys(self.scopeRow.maintainAccount.split(","));
				}
		    	self.loading = false
		  	})
		},
		checkSelect(data, checked, indeterminate){
			this.checkedArr = checked.checkedKeys;
		},
		quit(formName){
			if(this.checkedArr.length > 0){
				updateMaitainAccount({
					taskId:this.newTaskId,
					accounts:this.checkedArr.toString()
				}).then((data)=>{
					this.$message({
		              message: '指定维保人成功',
		              type: 'success'
		            });
					this.closeDialog(true)
				});
			}else{
				this.$message({
	              message: '请指定维保人',
	              type: 'warning'
	            });
			}
		}
	}
}
</script>